package cn.tedu.sp02.controller;

import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp01.service.ItemService;
import cn.tedu.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@RestController
@Slf4j
public class ItemController {
    @Autowired
    private ItemService itemService;

    @GetMapping("/{orderId}")
    public JsonResult<List<Item>> getItems(@PathVariable String orderId) throws Exception{
        List<Item> items = itemService.getItems(orderId);
        JsonResult data = JsonResult.ok().data(items);
            //90%概率执行延迟
        if (Math.random()<0.9){
            //随机延迟时长5s
            int i = new Random().nextInt(5000);
            log.info("随机延迟----- "+i);
            Thread.sleep(i);
        }


        return data;
    }

    @PostMapping("/decreaseNumber")
    public  JsonResult<?> decreaseNumber(@RequestBody List<Item> list){
        itemService.decreaseNumber(list);
        return JsonResult.ok().msg("减少商品库存成功");
    }
}
